// pages/order/order.js
var aduCartUrl = require('../../config').aduCartUrl;
var app = getApp();
Page({

  data: {
    currentTab:0,
    // 加入购物车
    sortAnimtead: {},
    animtedata: '',
    boxShow: false,
    payShow: false,
    cartShow: true,
    commNum:1,

    categroy:[],
    allDishList:{}
  },
  onLoad: function (options) {
    var that = this;
    var hasCategroy=0;
    var dishList = app.data.dishList;
    var categroy=["全部"]
    for(var i=0;i<dishList.length;i++){
      hasCategroy = 0;
      for(var j=0;j<categroy.length;j++){
        if (dishList[i].dish.dishType === categroy[j]){
          hasCategroy=1;
          break;
        }
      }
      if (hasCategroy===0){
        categroy.push(dishList[i].dish.dishType)
      }
    }
   
    that.setData({
      categroy: categroy,
      allDishList:dishList,
      showList: dishList
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that = this;
    that.closeBox()
  },
  // 菜品nav
  tabNav: function(e) {
    var that = this;
    var dishList = that.data.allDishList;
    var index = e.currentTarget.dataset.type;
    var categroyNow = that.data.categroy[index];
    var showList =[];
    if (categroyNow==='全部'){
      showList=dishList;
    }else{
      for (var i = 0; i < dishList.length; i++) {
        if (dishList[i].dish.dishType === categroyNow) {
          showList.push(dishList[i])
        }
      }
    }
    that.setData({
      currentTab: index,
      showList: showList
    })
  },
  //
  doOrder: function (e) {
    var that = this ;
    var index = e.currentTarget.dataset.index;
    var showDish =that.data.showList[index];
    this.setData({
      boxShow: true,
      showDish: showDish
    });
    var animation = wx.createAnimation({
      duration: 800,
      timingFunction: 'ease',
    });
    this.animation = animation;
    animation.bottom('0').opacity(1).step();
    this.setData({
      animtedata: animation
    })
  },
  // 关闭
  closeBox: function () {
    var that = this;
    var animation = wx.createAnimation({
      duration: 800,
      timingFunction: 'ease',
    });
    this.animation = animation;
    animation.bottom('-500rpx').opacity(0).step();
    that.setData({
      animtedata: animation,
      commNum: 1
    })
    setTimeout(function () {
      that.setData({
        boxShow: false
      })
    }, 500)
  },
  /**
     *加减 
     */
  operNum: function (e) {
    var that = this;
    var oper = e.currentTarget.dataset.oper
    var num;
    if (oper == '-') {
      that.data.commNum == 1 ? num = 1 : num = that.data.commNum - 1
    } else {
      num = that.data.commNum + 1;
    }
    that.setData({
      commNum: num
    })
  },
  // 加入购物车
  addCart:function(e){
    var that = this;
    var id=e.currentTarget.dataset.id;
    wx.request({
      method:'post',
      url: aduCartUrl ,
      data:{
        skey:wx.getStorageSync("skey"),
        dishId:id,
        dishNum: that.data.commNum,
        state:0
      },
      success:function(res){
        if(res.data.code==='ok'){
          that.closeBox();
          wx.showModal({
            title: '提示',
            content: '添加购物车成功',
            confirmText:'购物车',
            confirmColor:'#f60',
            cancelText:'继续点餐',
            success:function(res){
              if (res.confirm){
                wx.navigateTo({
                  url: '../cart/cart',
                })
              }
            },
            fail:function(){}
          })
        }
      }
    })
  },
  detail:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    wx.setStorageSync("showDishDetails", that.data.showList[index]);
    wx.navigateTo({
      url: '../productDetails/productDetails',
    })
  }
}) 